Thread: How to pass an array of strings by reference?`

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    16

    How to pass an array of strings by reference?`

    Hello,

    I'm trying to pass an array of strings to a function by reference and can't seem to figure out how to do it. Here is what I'm tring to do.

    #include <string>

    void goodbye(string &ra[]);

    int main();
    {
    int lines = 3;
    string a[lines];

    string a[0] = "Hello";
    string a[1] = "there";

    goodbye(a);
    return 0;
    }

    void goodbye(string &ra[])
    {
    ra[0] = "goodbye";
    ra[1] = "";
    }


    I know this isn't correct, but this is basically what I'm trying to do. Can anyone patch up this so that it would work or point me to a page with an explanation/tutorial? As of yet, google has failed me on this one.

    Thanks in advance,
    James1

  2. #2
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    If you have power point then dload this small file and go to page 21, 22, and 23:

    www.d.umn.edu/~rmaclin/cs1622/C++OtherFeatures/ C++OtherFeatures.PPT

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Reading that presentation didn't answer the question in my opinion. It had some nice use of arrays, but didn't address the issue of passing a string array to a function by reference. As for myself, I must admit I cant see why the example by James shouldn't work.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  4. #4
    stovellp
    Guest
    I'm not that sharp on pointers, so don't bother writing back if I'm wrong, but wouldn't "goodbye()" take a pointer as its argument, and the when you call it you'd assign it the address of an array, like:

    Code:
    void goodbye(string * str[])
        {
        *str[0] = "Fair dinkum!";
        }
    
    int main()
        {
        string myString[10];
        ...
        goodbye(&myString);
        }
    As I said, I'm probably wrong, so don't bother telling me I'm wrong. If by chance Im right though, please tell me

  5. #5
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    stovellp, you would be right, but sometimes it's just easier to use references, because then there is no tedious dereferencing a mucking about. The variable you obtain by reference is exactly identical in structure and usability to the one passed in. And also, there shouldn't be square brackets for passing a pointer. That would pass an array of pointers. Sorry, I just had to correct. It's all a learning experience.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  6. #6
    stovellp
    Guest

    Thanks!

    Wow, I've spent the last two years avoiding pointers because I didn't think I knew how to use them, just to be told I was right. Thanks mate!

    And yeah, I wasn't sure about the '[]'s, I realise now they shouldn't be there because It would pass the whole array.

  7. #7
    stovellp
    Guest

    Hold on...

    I read that wrong... Whats the difference between a reference and a pointer? I thought what I was doing was assigning the address to a pointer, so everything changed in the pointer would be changed in the real variable whose address was passed to it?

    I'm lost again

  8. #8
    Registered User nag's Avatar
    Join Date
    May 2002
    Posts
    22
    void goodbye(string &ra[])

    void goodbye(string * str[])
    Amazing!! ,men when you pass an array to a function it is always passed as a pointer to its first element,so you should pass the size of array as its second parameter ,what you are trying to do can be simply attained by putting

    void goodboye(string str[])

    This will pass the base address of array to the function but you need to tell the actual size of array ,so the complete code would be


    void goodboye(string str[],int lines)

    Also if you do not want to change the array size in the function or you afraid that it would be changed unintentionally you should pass its second parameter as a constant.
    Last edited by nag; 01-24-2003 at 06:58 AM.

  9. #9
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Ok, here's the deal; I'll explain it a little here, but you should really just do a search or read a tutorial.

    A pointer is a (usually) 4 byte variable that holds a memory address. You can use the address operator, &, to obtain the address of a variable, and place that in the pointer. Now you can manipulate the variable THROUGH the pointer. For example:
    Code:
    int variable; //create an int, pretty standard ;)
    int *ptrtoint; //create a pointer that is going to point to an int
    
    ptrtoint=&variable; //assign address of variable to ptrtoint
    
    *ptrtoint=5; //The dereference operator is the asterisk.
    //What this does is sort of resolves the pointer so 
    //that you can read and write the value in the 
    //variable that it points to.
    Now you may say, what's the point of using pointers, I can already access the variable directly. Pointers come in handy when you need to access a variable that was created by another part of your program, or the operating system. For example, if you want a function to be able to change a variable that you pass to it, you pass a pointer, so that the function knows where the variable is stored in memory. Ie:

    Code:
    void increment(int *var)
    {
         (*var)++; //(value pointed to by var)increment
    //Note that parentheses are necessary for precedence reasons
    }
    
    void main() //uh oh, salem's gonna kill me
    {
    int a;
    a=5;
    increment(&a); //pass the address of the variable
    cout << a << endl; //will display 6, not 5
    }
    If you were to pass a variable normally, by value, a copy of the variable would be created in the context of the function increment(), and any changes made to it would not effect the original variable.

    Well, that's all I can explain right now, you should really get a tutorial. Start here:

    http://newdata.box.sk/bx/c/index.htm

    That's how I learnt everything I know.

    Plus, I'm a really bad teacher, and it's about 40 degrees down here in aussie
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  10. #10
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Are you sure nag? I would think that those parameters would require you to pass an array, not a pointer to one.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  11. #11
    Registered User nag's Avatar
    Join Date
    May 2002
    Posts
    22
    But I think the actual question was How to pass an array of strings to a function ?
    And also no matter if you pass an array as pointer,the function does not get a new copy of array as usual It can change the array because it is passed as a pointer!
    Last edited by nag; 01-24-2003 at 07:03 AM.

  12. #12
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Actually, I think the question was How to pass an array of strings by reference?

    While waiting for someone else to answer that post, I took the liberty of helping out stovellp with some essentials of c++ programming. I'm gradually getting around to answering the original post, don't worry.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  13. #13
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Nag, I concede, your function parameter line is perfectly adequate. The following are exactly the same:

    [code]
    void goodbye(string str[]);
    void goodbye(string *str);
    [code]

    I believe however that using the second definition would be more facilitating to a better understanding of the workings of the program. It indicates the variable to be passed in contains an address that is the location of one or more strings. The first definition implies that an array of strings is being passed in, which is misleading. That is because it could mean either a pointer to an array or an actual array.

    However, do what you want.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  14. #14
    Registered User
    Join Date
    Dec 2002
    Posts
    103

    This should answer the actual question

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    void pass_by_reference(string (&ref)[10])
    {
           ref[0] = "This is string one";
           // this is how you take a reference for an array
    }
    
    int main()
    {
          string str[10];
          pass_by_reference(str);
          return 0;
    }

    to make it simple....
    int x[10]; // if you wanted a reference to this array of integers

    int (&ref)[10] = x; // this is how you take the reference of the array x
    Have a wonderful day.... and keep smiling... you look terrific that way
    signing off...
    shiv... as i know him

  15. #15
    Registered User
    Join Date
    Dec 2002
    Posts
    103

    This should WORK

    Originally posted by JamesMI
    Code:
    #include <string>
    
    const int MAX = 3; // added this instead
    
    void goodbye(string (&ra)[MAX]); // Corrections here
    
    int main();
    {
         string a[MAX];
    
         string a[0] = "Hello";
         string a[1] = "there";
    
         goodbye(a);
         return 0;
    }
    
    void goodbye(string (&ra)[3]) // Corrections here
    {
         ra[0] = "goodbye";
         ra[1] = "";
    }
    Have a wonderful day.... and keep smiling... you look terrific that way
    signing off...
    shiv... as i know him

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. array of strings?
    By mc72 in forum C Programming
    Replies: 5
    Last Post: 11-16-2008, 12:15 AM
  3. Replies: 2
    Last Post: 04-27-2008, 03:39 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. array of strings + more
    By null in forum C Programming
    Replies: 10
    Last Post: 10-01-2001, 03:39 PM